//
//  MKUILedBannerVerticalView.m
//  MKUI
//
//  Created by 苏尚进 on 2021/12/29.
//  Copyright © 2021 moon. All rights reserved.
//
#import "MKUICollectionViewPageFlowLayout.h"
#import "MKUILedBannerVerticalView.h"
#import "MKUICollectionView.h"
#import "MKUIPreCompile.h"
#import "MKUICollectionViewCellBase.h"

@interface MKUILedBannerVerticalView ()<MKUICollectionViewDelegatePageFlowLayout>
@property(nonatomic,strong) UIView *contentView;
@property(nonatomic,strong) MKUICollectionView *collectionView;
@property(nonatomic,strong) MKUICollectionViewPageFlowLayout *pageFlowLayout;
@end

@implementation MKUILedBannerVerticalView
- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.defaultContentCellClass = [MKUICollectionViewCellBase class];
        self.contentView = [[UIView alloc] init];
        [self addSubview:self.contentView];
        
        self.pageFlowLayout = [[MKUICollectionViewPageFlowLayout alloc] init];
        self.pageFlowLayout.pagingEnabled = YES;
        self.pageFlowLayout.enableCycleScroll = YES;
        self.pageFlowLayout.itemAlignment = MKCGRectAlignmentMin;
        self.pageFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        self.pageFlowLayout.interitemSpacing = 0;
        self.pageFlowLayout.sectionInset = UIEdgeInsetsMake(self.pageFlowLayout.interitemSpacing*0.5, 0, self.pageFlowLayout.interitemSpacing*0.5, 0);
        self.pageFlowLayout.pagingCellPosition = 0.5;
        self.pageFlowLayout.pagingBoundsPosition = 0.5;
        
        self.collectionView = [[MKUICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:self.pageFlowLayout];
        self.collectionView.scrollsToTop = NO;
        self.collectionView.showsVerticalScrollIndicator = NO;
        self.collectionView.showsHorizontalScrollIndicator = NO;
        self.collectionView.backgroundColor = [UIColor clearColor];
        self.collectionView.scrollEnabled = NO;
        self.collectionView.model.forwardDelegate = self;
        [self.contentView addSubview:self.collectionView];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.bounds;
    self.contentView.frame = bounds;
    {
        CGRect bounds = self.contentView.bounds;
        BOOL frameChange = !CGSizeEqualToSize(bounds.size, self.collectionView.frame.size);
        self.collectionView.frame = bounds;
        if(frameChange){
            [self.collectionView reloadData];
        }
    }
}
- (void)__reloadData{
    [self.collectionView.model removeAllSectionModels];
    @MK_WEAKIFY(self);
    for (int i=0; i<self.contents.count; i++) {
        NSObject *obj = self.contents[i];
        Class cellClass = self.defaultContentCellClass;
        if([self.delegate respondsToSelector:@selector(ledBannerVerticalView:cellClassForBannerContent:)]){
            cellClass = [self.delegate ledBannerVerticalView:self cellClassForBannerContent:obj];
        }
        if(!cellClass){
            cellClass = self.defaultContentCellClass;
        }
        MKUICollectionViewCellModel *cm = [MKUICollectionViewCellModel modelWithValue:obj cellClass:cellClass whenClick:^(__kindof MKUICollectionViewCellModel * _Nonnull cellModel) {
            @MK_NORMALIZE(self);
            if([self.delegate respondsToSelector:@selector(ledBannerVerticalView:didClickBannerWithIndex:bannerContent:)]){
                [self.delegate ledBannerVerticalView:self didClickBannerWithIndex:i bannerContent:obj];
            }
        }];
        [self.collectionView.model addCellModel:cm];
    }
    [self.pageFlowLayout reloadData];
    [self.collectionView.model reloadCollectionViewData];
    
    if(self.currentIndex>=0 && self.currentIndex < self.contents.count){
        [self.pageFlowLayout setIndexPathAtPagingCell:[NSIndexPath indexPathForItem:self.currentIndex inSection:0] animated:NO];
    }
}
- (void)reloadData{
    [self __reloadData];
}
- (void)setContents:(NSArray<NSObject *> *)contents{
    _contents = contents;
    [self reloadData];
}
- (NSInteger)currentIndex{
    NSInteger currentIndex = NSNotFound;
    NSIndexPath *indexpath = self.pageFlowLayout.indexPathAtPagingCell;
    if(indexpath){
        currentIndex = indexpath.item;
    }
    return currentIndex;
}
- (void)setCurrentIndex:(NSInteger)currentIndex animated:(BOOL)animated{
    if(currentIndex>=0 && currentIndex<self.contents.count){
        NSIndexPath *indexpath = [NSIndexPath indexPathForItem:currentIndex inSection:0];
        [self.pageFlowLayout setIndexPathAtPagingCell:indexpath animated:animated];
    }
}
- (void)setCurrentIndex:(NSInteger)currentIndex{
    [self setCurrentIndex:currentIndex animated:NO];
}
- (void)setCurrentIndexWithDistance:(NSInteger)distance animated:(BOOL)animated{
    [self.pageFlowLayout setIndexPathAtPagingCellWithDistance:distance animated:animated];
}
- (BOOL)isAutoScrolling{
    return self.pageFlowLayout.isAutoScrolling;
}
- (void)startAutoScrollingWithDistance:(NSInteger)distance duration:(NSTimeInterval)duration{
    [self.pageFlowLayout startAutoScrollingWithDistance:distance duration:duration];
}
- (void)stopAutoScrolling{
    [self.pageFlowLayout stopAutoScrolling];
}
- (void)dealloc{
    
}
#pragma mark - delegate:MKUICollectionViewDelegatePageFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView pageFlowLayout:(MKUICollectionViewPageFlowLayout *)collectionViewLayout itemSizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return collectionView.bounds.size;
}
- (void)collectionView:(UICollectionView *)collectionView pageFlowLayout:(MKUICollectionViewPageFlowLayout *)collectionViewLayout didScrollToPagingCell:(NSIndexPath *)indexPathAtPagingCell{
    if(indexPathAtPagingCell.item>=self.contents.count){//出现这种情况是进行了reload操作，新的contents少于原来的contents
        return;
    }
    if([self.delegate respondsToSelector:@selector(ledBannerVerticalView:didScrollToBannerWithIndex:)]){
        [self.delegate ledBannerVerticalView:self didScrollToBannerWithIndex:indexPathAtPagingCell.item];
    }
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.item>=self.contents.count){//出现这种情况是进行了reload操作，新的contents少于原来的contents
        return;
    }
    if([self.delegate respondsToSelector:@selector(ledBannerVerticalView:willDisplayCell:fonContent:)]){
        id content = self.contents[indexPath.row];
        [self.delegate ledBannerVerticalView:self willDisplayCell:cell fonContent:content];
    }
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.item>=self.contents.count){//出现这种情况是进行了reload操作，新的contents少于原来的contents
        return;
    }
    if([self.delegate respondsToSelector:@selector(ledBannerVerticalView:didEndDisplayingCell:fonContent:)]){
        id content = self.contents[indexPath.row];
        [self.delegate ledBannerVerticalView:self didEndDisplayingCell:cell fonContent:content];
    }
}
@end
